package com.lifang.imsoa.util;

import com.lifang.imsoa.model.request.AppNameEnum;
import com.lifang.redis.keyenum.KeyGenInterface;
import com.lifang.redis.keyenum.KeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/12/3.
 */
@Slf4j
public enum RedisKey implements KeyGenInterface{
    hxUserStatus("imsoa:hxUserStatus");//xfsoa(service) -->estatesub(module)

    String moduleKey;

    RedisKey(String modelKey){
        this.moduleKey = modelKey;
    }
    @Override
    public String getModuleKey() {
        return moduleKey;
    }
    @Override
    public String key(String... keys) {
        return KeyUtil.genKey(moduleKey, keys);
    }
    @Override
    public String key(String subModule, String key) {
        return KeyUtil.genKey(moduleKey, subModule, key);
    }

    /**
     * 得到用户ID集合对应的keys
     * @param userIds 用户ID
     * @return
     */
    public static List<String> getKey(List<String> userIds, String appName) {
        List<String> resultList = new ArrayList<>();
        for (String userId : userIds) {
            resultList.add(hxUserStatus.key(appName+userId));
        }
        return resultList;
    }

    /**
     * 拆分key，去掉前缀，得到hxId
     */
    public static String getHxIdBySplitKey(String key){
        if(StringUtils.isNotBlank(key)){
            return key.substring(19);
        }
        log.error("key非法,key={}",key);
        return null;
    }
}
